package com.sk.coin.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.coin.sys.entity.SysUser;
import com.sk.coin.sys.entity.SysUserRole;
import com.sk.coin.sys.mapper.SysUserMapper;
import com.sk.coin.sys.service.ISysUserRoleService;
import com.sk.coin.sys.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台用户 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-02-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public Page<SysUser> findByPage(Page<SysUser> page, String mobile, String fullName) {
        Page<SysUser> userPage = page(page, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.isNotBlank(mobile), SysUser::getMobile, mobile)
                .like(StringUtils.isNotBlank(fullName), SysUser::getFullname, fullName));
        List<SysUser> records = userPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (SysUser record : records) {
                List<SysUserRole> userRoleList = sysUserRoleService.lambdaQuery()
                        .eq(SysUserRole::getUserId, record.getId()).list();
                if (!CollectionUtils.isEmpty(userRoleList)) {
                    record.setRole_strings(userRoleList.stream()
                            .map(sysUserRole -> sysUserRole.getRoleId().toString())
                            .collect(Collectors.joining(",")));
                }
            }
        }
        return userPage;
    }

    @Override
    @Transactional
    public boolean addUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        String role_strings = sysUser.getRole_strings();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        // 加密密码
        String encode = bCryptPasswordEncoder.encode(password);
        sysUser.setPassword(encode);
        boolean save = save(sysUser);
        if (StringUtils.isNotEmpty(role_strings)) {
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            String[] roleIds = role_strings.split(",");
            for (String roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(Long.valueOf(roleId));
                sysUserRole.setUserId(sysUser.getId());
                sysUserRoleList.add(sysUserRole);
            }
            sysUserRoleService.saveBatch(sysUserRoleList);
        }
        return save;
    }

    @Override
    public boolean delectByIds(List<Long> list) {
        boolean b = removeByIds(list);
        sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, list));
        return b;
    }

}
